package leetcode.code0861;

public class Solution {
	public int matrixScore(int[][] grid) {
		int m = grid.length, n = grid[0].length;
		for (int i = 0; i < m; i++) {
			if (grid[i][0] == 0) {
				for (int j = 0; j < n; j++) {
					grid[i][j] = 1 - grid[i][j];
				}
			}
		}
		int sum = m * (1 << (n - 1));
		for (int i = 1; i < n; i++) {
			int cnt = 0;
			for (int j = 0; j < m; j++) {
				cnt += grid[j][i];
			}
			cnt = Math.max(cnt, m - cnt);
			sum += cnt * (1 << (n - 1 - i));
		}
		return sum;
	}
}
